package com.hex.liyue.lookingdatemodule.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hex.liyue.LiYueApplication;
import com.hex.liyue.R;
import com.hex.liyue.commonmodule.glide.GlideCircleTransform;
import com.hex.liyue.commonmodule.utils.DateUtils;
import com.hex.liyue.lookingdatemodule.bean.HotNewResultBean;

import java.text.DecimalFormat;
import java.util.List;

/**
 * Created by WJ on 2016/6/12.
 */
public class AdapterHotNew extends RecyclerView.Adapter<AdapterHotNew.MyHolder> {
    private OnItemClickListener onItemClickListener;
    private List<HotNewResultBean.DataBean> list;

    public AdapterHotNew(List<HotNewResultBean.DataBean> list) {
        this.list = list;
    }

    public interface OnItemClickListener {
        void onItemClickListener(String appointment_id);
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(parent.getContext(), R.layout.item_looking_date, null);
        return new MyHolder(view);
    }

    @Override
    public void onBindViewHolder(MyHolder holder, final int position) {
        HotNewResultBean.DataBean dataBean = list.get(position);
        Glide.with(LiYueApplication.getInstance())
                .load(dataBean.launch_user.avatar)
                .error(R.drawable.icon_no_thing)
                .transform(new GlideCircleTransform(LiYueApplication.getInstance()))
                .into(holder.iv_avater);
        long birthday = DateUtils.getAge(dataBean.launch_user.birthday * 1000);
        if (birthday == 0) {
            holder.tv_name.setText(dataBean.launch_user.nickname);
        } else {
            holder.tv_name.setText(dataBean.launch_user.nickname + "   " + birthday);
        }

        holder.tv_begin_date.setText(DateUtils.getFormatDate(dataBean.invitations_time * 1000, "yyyy年MM月dd日 HH:mm"));
        holder.tv_location.setText(dataBean.location);
        holder.tv_event.setText(dataBean.event);
        holder.tv_number.setText(dataBean.invitations_real_number + "人约");
        holder.tv_gift_total.setText("收到￥" +qianweifenge(Long.parseLong(dataBean.gift_total)) );
        holder.rl_item_looking_date.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClickListener(list.get(position).appointment_id);
                }
            }
        });
    }
    public static String qianweifenge(long num) {
        DecimalFormat df = new DecimalFormat("#,###");
        String ss = df.format(num);
        return ss;
    }
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class MyHolder extends RecyclerView.ViewHolder {
        ImageView iv_avater;
        TextView tv_name, tv_begin_date, tv_location, tv_event, tv_number, tv_gift_total;
        RelativeLayout rl_item_looking_date;

        public MyHolder(View itemView) {
            super(itemView);
            rl_item_looking_date = (RelativeLayout) itemView.findViewById(R.id.rl_item_looking_date);
            iv_avater = (ImageView) itemView.findViewById(R.id.iv_avater);
            tv_name = (TextView) itemView.findViewById(R.id.tv_name);
            tv_begin_date = (TextView) itemView.findViewById(R.id.tv_begin_date);
            tv_location = (TextView) itemView.findViewById(R.id.tv_location);
            tv_event = (TextView) itemView.findViewById(R.id.tv_event);
            tv_number = (TextView) itemView.findViewById(R.id.tv_number);
            tv_gift_total = (TextView) itemView.findViewById(R.id.tv_gift_total);
        }
    }
}
